//
//  UICollectionView+GJKit.m
//  YDGJ
//
//  Created by 1dcq on 16/6/22.
//  Copyright © 2016年 Galaxy360. All rights reserved.
//

static char const * const GJTipsCallBackBlock =          "GJTipsCallBackBlock";


#import "UICollectionView+GJKit.h"
#import "FD_Method.h"

#define GJTipsBtnHeight (30)

@implementation UICollectionView (GJKit)

+(instancetype)collectionViewWithFrame:(CGRect )rect collectionViewLayout:(UICollectionViewLayout *)layout registerCellClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(Screen_Width*0.5, Screen_Width*0.5);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 0.f;
    flowLayout.minimumInteritemSpacing = 0.f;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:nil == layout?flowLayout:layout];
    collectionView.backgroundColor = Color_BackGround;
    collectionView.contentInset = UIEdgeInsetsMake(kTableViewTopSpace, 0, 0, 0);
    collectionView.delaysContentTouches = NO;
    collectionView.alwaysBounceVertical = YES;
    [collectionView registerClass:nil == cellClass?[UICollectionViewCell class]:cellClass forCellWithReuseIdentifier:identifier];
    
    return collectionView;
}

-(void)setTips:(NSString *)tips AdTipsType:(GJCollectionViewTipsType)tipType handTipsTouchBlock:(tipsTouchBlock)block
{
    if (!tips.length)
    {
        [self removeTipsView];
        return;
    }
    UIView *tipsView = [[UIView alloc]initWithFrame:self.bounds];
    tipsView.backgroundColor = [UIColor clearColor];
    if (block)
    {
        objc_setAssociatedObject(self, GJTipsCallBackBlock, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    UIButton *tipsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIFont *btnFont = FontCustomSize(15.f);
    CGFloat btnWidth = [FD_Method stringSize:tips andFont:btnFont andwidth:self.bounds.size.width].width;
    tipsBtn.frame = CGRectMake((self.width - btnWidth)*0.5, (self.height - GJTipsBtnHeight)*0.5, btnWidth, GJTipsBtnHeight);
    [tipsBtn setTitle:tips forState:UIControlStateNormal];
    [tipsBtn setTitleColor:UIColorFromRGB(0xa4a4a4) forState:UIControlStateNormal];
    [tipsView addSubview:tipsBtn];
    self.backgroundView = tipsView;
    
    if (GJCollectionViewTipsTypeEmptyData == tipType)
    {
        tipsBtn.userInteractionEnabled = NO;
        return;
    }
    [tipsBtn addTarget:self action:@selector(tipsBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)tipsBtnClicked:(UIButton *)sender
{
    tipsTouchBlock touchBlock = objc_getAssociatedObject(self, GJTipsCallBackBlock);
    if (touchBlock) touchBlock();
    [self removeTipsView];
}

-(void)removeTipsView
{
    [self.backgroundView removeFromSuperview];
    self.backgroundView = nil;
}


@end
